package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by mz on 14/9/17.
 *
 */
@Data
public class RetailImmunizationRecord implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -5865576281286471452L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 单据编号
     */
    private String number;

    /**
     * 村ID，可以为空
     */
    private Long villageId;

    /**
     * 村，可以为空
     */
    private String village;

    /**
     * 畜主
     */
    private String name;

    /**
     * 畜主电话
     */
    private String mobile;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场名称
     */
    private String breedFactoryName;

    /**
     *
     */
    private Integer stock;

    /**
     *
     */
    private Double acreage;

    /**
     * 畜种
     */
    private Integer breedType;

    /**
     * 畜种单位
     */
    private Integer breedUnit;

    /**
     * 耳标号
     */
    @JsonIgnore
    private String earNumberJson;

    /**
     * 耳标号
     */
    private List<String> earNumbers;

    /**
     * 状态 0 、正常 1、生效
     */
    private Integer status;

    /**
     * 单据是否有效
     */
    private Integer deleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


    public void setEarNumberJson(String earNumberJson){
        this.earNumberJson = earNumberJson;

        if(Strings.isNullOrEmpty(earNumberJson)){
            this.earNumbers = new ArrayList<>();
        }else {
            try {
                this.earNumbers =
                        objectMapper.readValue(earNumberJson, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setEarNumbers(List<String> earNumbers){
        this.earNumbers = earNumbers;

        if(earNumbers == null || earNumbers.isEmpty()){
            this.earNumberJson = null;
        }else {
            try {
                this.earNumberJson =
                        objectMapper.writeValueAsString(earNumbers);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }
}
